a = int(input())
s = []
s.append(a)
a = str(a)
b = []
if len(str(a)) != 4 or int(a) < 0:
    print(0)
else:
    for i in range(4):
        s.append(int(a[i]))
        if i != 3:
            s.append(int(a[i:i+2]))
        if i != 2:
            s.append(int(a[i:i + 3]))
    for i in s:
         for j in range(2, i):
            if i % j == 0:
               break
         else:
             b.append(i)
    if 1000 > int(a) or int(a) > 9999 or b == []:
         print(0)
    else:
         print(max(b))

